﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyBanHang_NS
{
    public partial class QuanLyBanHang : Form
    {
        public QuanLyBanHang()
        {
            InitializeComponent();
        }        
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.BeginEdit(true);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {
            
        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            String name = tabControl1.TabPages[e.Index].Text;
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;
            f.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(name,this.Font,Brushes.Black,e.Bounds,f);

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void tb_tongtien_TextChanged(object sender, EventArgs e)
        {

        }

        private void QuanLyBanHang_Load_1(object sender, EventArgs e)
        {            
            QuanLyNhaSach.BusinessLogicLayer.QuanLySachBLL qls = new QuanLyNhaSach.BusinessLogicLayer.QuanLySachBLL();
            DataTable data = qls.LayDanhSach();
            lb_danhsachsach.DataSource = data;
            lb_danhsachsach.DisplayMember = "TenSach";
            lb_danhsachsach.ValueMember = "MaSach";
            QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL qlbh = new QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL();
            tb_mahd.Text = qlbh.TaoMaHoaDon();

            QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL qlkh = new QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL();            
            tb_makh2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tb_makh2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL kh = new QuanLyNhaSach.BusinessLogicLayer.QuanLyKhachHangBLL();
            tb_makh2.AutoCompleteCustomSource = kh.TaoAutocompleteMa();
            
            //tb_makh.Text = qlkh.TaoMaKhachHang();
            tb_makh.Text = "K0000";
            tb_makh.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tb_makh.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tb_makh.AutoCompleteCustomSource = kh.TaoAutocompleteMa();

            
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void lb_danhsachsach_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lb_danhsachsach_DoubleClick(object sender, EventArgs e)
        {
             
             QuanLyNhaSach.BusinessLogicLayer.QuanLySachBLL qls = new QuanLyNhaSach.BusinessLogicLayer.QuanLySachBLL();
             DataTable data = qls.XemThongTinSach(lb_danhsachsach.SelectedValue.ToString());           
             dataGridView1.Rows.Add(data.Rows[0]["MaSach"], data.Rows[0]["TenSach"], data.Rows[0]["TheLoai"], "1", qls.LayGiaBan(data.Rows[0]["MaSach"].ToString()).ToString());
             float tongtien = 0;                        
             foreach (DataGridViewRow a in dataGridView1.Rows)
             {
                 if (a.Index == dataGridView1.Rows.Count - 1)
                     continue;
                 tongtien += int.Parse(a.Cells["soluong"].Value.ToString()) * int.Parse(a.Cells["dongia"].Value.ToString());
             }
             tb_tongtien.Text = tongtien.ToString();            
        }
        

        private void bt_layhoadon_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            string makh;
            makh = tb_makh2.Text;
            QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL qlbh = new QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL();
            DataTable data = qlbh.LayHoaDon(makh);
            int tongno = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                int tienhd=0;
                DataTable data2 = qlbh.LayCTHD(data.Rows[i]["MaHoaDon"].ToString());
                tienhd = qlbh.TongTienCuaHoaDon(data.Rows[i]["MaHoaDon"].ToString());
                tongno += tienhd;
                dataGridView2.Rows.Add(data.Rows[i]["MaHoaDon"], tienhd, 0);           
            }
            tb_tongno2.Text = "";
            tb_tongno2.Text += tongno.ToString();

        }

        private void tb_tienktra_TextChanged(object sender, EventArgs e)
        {           
            float tongtien = 0;
            foreach (DataGridViewRow a in dataGridView1.Rows)
            {
                if (a.Index == dataGridView1.Rows.Count - 1)
                    continue;
                tongtien += int.Parse(a.Cells["soluong"].Value.ToString()) * int.Parse(a.Cells["dongia"].Value.ToString());
            }
            if (tb_tienktra.Text == null||tb_tienktra.Text=="")
            {
                tb_tienktra.Text = "0";
                tb_tiendu.Text = "-" + tongtien.ToString();                
            }
            else
            {
                int tienktra = int.Parse(tb_tienktra.Text);
                int tiendu = tienktra - (int)tongtien;
                tb_tiendu.Text = tiendu.ToString();
            }
            if (int.Parse(tb_tienktra.Text) > int.Parse(tb_tongtien.Text))
            {
                cb_thanhtoan.Checked = true;
            }
            else 
            {
                cb_thanhtoan.Checked = false;
            }
        }

        private void bt_luuhoadon_Click(object sender, EventArgs e)
        {
            QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL q = new QuanLyNhaSach.BusinessLogicLayer.QuanLyBanHangBLL();
            if (cb_thanhtoan.Checked == true)
            {
                q.LuuHoaDon(tb_mahd.Text, tb_makh.Text, DateTime.Now.ToString(), "1");
            }
            else { 
                q.LuuHoaDon(tb_mahd.Text,tb_makh.Text,DateTime.Now.ToString(),"0");
            }            
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Index == dataGridView1.Rows.Count - 1)
                    continue;
                q.LuuChiTietHoaDon(tb_mahd.Text,r.Cells["MaSach"].Value.ToString(),int.Parse(r.Cells["soluong"].Value.ToString()),int.Parse(r.Cells["dongia"].Value.ToString()));
            }
            if (int.Parse(tb_tienktra.Text) >= int.Parse(tb_tongtien.Text))
            {
                q.ThemPhieuThu(tb_mahd.Text, DateTime.Now.ToString(), tb_tongtien.Text);                
            }
            else
            {
                q.ThemPhieuThu(tb_mahd.Text,DateTime.Now.ToString(),tb_tienktra.Text);
            }                    
            MessageBox.Show("da luu hoadon va cthd");
            dataGridView1.Rows.Clear();           
            tb_makh.Text = "";
            tb_tiendu.Text = "";
            tb_tienktra.Text = "";
            tb_tongtien.Text = "";           
            tb_mahd.Text = q.TaoMaHoaDon();
        }

        private void cb_thanhtoan_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void tb_tienktra2_TextChanged(object sender, EventArgs e)
        {            
            int tienktra = 0;
            int tiendu = 0;
            int tongtien = 0;
            foreach (DataGridViewRow r in dataGridView2.Rows)
            {
                if (r.Index == dataGridView2.Rows.Count-1)
                {
                    continue;
                }
                tongtien += int.Parse(r.Cells["tongtien"].Value.ToString());
            }
            if (tb_tienktra2.Text == null || tb_tienktra2.Text == "")
            {
                tb_tienktra2.Text = "0";
            }
            else {
                tienktra = int.Parse(tb_tienktra2.Text);
                tiendu = tienktra - tongtien;
                lb_tienthua2.Text = tiendu.ToString();
            }

        }
    }
}
